#coding=utf-8
import os,re,logging,datetime,time,cgi,urllib,time, datetime
import wsgiref.handlers
from google.appengine.ext import webapp
from ad import AdReceiver
from mail import MailSender, MailSender2, MailSender3, MailHandler, PostRejected,get_sender, change_sender
from counter import CounterStatus
from google.appengine.api.labs import taskqueue

class Controler(webapp.RequestHandler):
  def get(self):
    now = datetime.datetime.now()
    
    h = now.hour
    n = 0
    if h in range(2,5):
      n = 3
    elif h in range(8, 14):
      n = 4
    else:
      pass
      
    #logging.info('%i'%n)
     
    for i in range(n):
      taskqueue.add(url='/send_mail2', method='GET')
      time.sleep(0.1)
    
'''
class Sender1(webapp.RequestHandler):
  def get(self):
    self.response.out.write(get_sender())
    
class Sender2(webapp.RequestHandler):
  def get(self):
    change_sender()
    self.response.out.write(get_sender())
'''    
application = webapp.WSGIApplication([
  ('/send_mail', MailSender),
  ('/send_mail2', MailSender2),
  ('/send_mail3', MailSender3),
  
  ('/receive_ad', AdReceiver),
  
  ('/count', CounterStatus),
  
  ('/post_rejected', PostRejected),
  ('/control', Controler),
  
  #('/get_sender', Sender1),
  #('/change_sender', Sender2),
  
  MailHandler.mapping(),
  
], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()
